import * as Redux from 'redux';
import thunk from 'redux-thunk';
import {
    createReactNavigationReduxMiddleware,
} from 'react-navigation-redux-helpers';
import {
    reducer,
    StoreState,
} from './reducer';
import { Platform } from 'react-native';
import {
    createFetchPromiseMiddleware,
    setGenerateUUID,
    setPayloadKeyFun,
    setPostKeyFun,
} from './middleware/fetchpromise';
import * as utils from '../utils';
import urls from '../constants/urls';

setGenerateUUID(() => utils.generateUUID('fetchPromise'));
// setPayloadKeysFun('uid',(store:StoreState)=>store.model.user.uid);
setPayloadKeyFun('url', () => urls.APIURL);

setPostKeyFun('appid', () => 109);
// setPostKeyFun('access_token',);

const mList = [
    thunk,
    createFetchPromiseMiddleware(),
    createReactNavigationReduxMiddleware('root', (state: StoreState) => state.ui.nav),
];

let middleware = Redux.applyMiddleware(...mList);
if (__DEV__) {
    const devTool = require('remote-redux-devtools');
    const composeEnhancers = devTool.composeWithDevTools({
        name: Platform.OS,
        hostname: 'localhost',
        port: 5678,
    });
    middleware = composeEnhancers(middleware);
}
export const store = Redux.createStore(reducer, middleware);
